<?php

function iplocs($val)
{
    $url = "http://ip.taobao.com/service/getIpInfo.php?";
    $para = "ip=" . $val;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url . $para);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $output = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($output, true);
    return $data;
}

function shipname($ship)
{
    $shipping=M("shipping");
    $data=$shipping->find($ship);
    return $data['sip_name'];
}

function username($uid)
{
    $users=M("users");
    $data=$users->find($uid);
    return $data["uss_name"];
}

function getpages($db, $where, $limit=20, $order=null)
{
    $count = $db->where($where)->count();
    $Page = new \Think\Page($count, $limit);
    $show = $Page->show();
    $list = $db->where($where)->limit($Page->firstRow . "," . $Page->listRows)->order($order)->select();
    return array("data" => $list, 'page' => $show);
}

function i_array_column($input, $columnKey, $indexKey = null)
{
    if (!function_exists('array_column')) {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
        $indexKeyIsNull = (is_null($indexKey)) ? true : false;
        $indexKeyIsNumber = (is_numeric($indexKey)) ? true : false;
        $result = array();
        foreach ((array) $input as $key => $row) {
            if ($columnKeyIsNumber) {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
            } else {
                $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
            }
            if (!$indexKeyIsNull) {
                if ($indexKeyIsNumber) {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key)) ? current($key) : null;
                    $key = is_null($key) ? 0 : $key;
                } else {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    } else {
        return array_column($input, $columnKey, $indexKey);
    }
}

/**
 * 加密
 * @param type $data
 * @return type
 */
function encodepass($data)
{
    $so1 = md5($data);
    $so2 = base64_encode(substr($so1, 0, 28));
    $exp = md5(substr($so2, 8, strlen($so2)));
    return substr($exp, 0, 18);
}

//获取订单归属
function getordloc($ord)
{
    $order = M("order", "yub_", C("CONN_YYB"));
    $addr = M("addr", "yub_", C("CONN_YYB"));
    $region = M("region", "yub_", C("CONN_YYB"));

    $info = $order->where("ord_sn='" . $ord . "'")->find();
    if ($info == null) {
        return "无";
    }
    $adinfo = $addr->find($info['ord_addr']);
    $p = $region->find($adinfo["adr_province"]);
    $c = $region->find($adinfo["adr_city"]);
    $l = $region->find($adinfo["adr_area"]);
    return $p["ren_name"] . $c['ren_name'] . $l['ren_name'];
}

//获取订单数量
function getordnum($ord)
{
    $order = M("orderblg", "yub_", C("CONN_YYB"));
    $where["oeg_ordsn"] = $ord;
    $list = $order->where($where)->find();
    return $list["oeg_num"] == null ? 0 : $list["oeg_num"];
}

//获取订单信息
function getordinfo($ord, $colum)
{
    $order = M("order", "yub_", C("CONN_YYB"));
    $where["ord_sn"] = $ord;
    $list = $order->where($where)->find();
    return $colum == "ord_addtime" ? date("Y-m-d", $list[$colum]) : $list[$colum];
}
